/*
 * ShopCategoryPresenter.java
 * business
 *
 * Created by ChenTao on 2016/10/25.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.category;

import android.os.Bundle;

import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Tag;
import com.yidiandao.business.data.model.IShopModel;
import com.yidiandao.business.data.response.QueryShopCategoryResponse;
import com.yidiandao.business.ui.widget.view.TagView;

import java.util.List;

/**
 * 店铺分类 - Presenter
 */
public class ShopCategoryPresenter implements ShopCategoryContract.Presenter {

    private ShopCategoryContract.View mShopCategoryView;

    private IShopModel mShopModel;

    private String mSupplierId;

    private List<QueryShopCategoryResponse.ShopCategory> mShopCategoryList;

    public ShopCategoryPresenter(ShopCategoryContract.View view, IShopModel shopModel) {
        mShopCategoryView = view;
        mShopModel = shopModel;

        mShopCategoryView.setPresenter(this);
    }

    @Override
    public void start() {
        loadShopCategoryList();
    }

    @Override
    public void bundleSupplierId(Bundle bundle) {
        mSupplierId = bundle.getString(Intents.EXTRA_SUPPLIER_ID);
    }

    @Override
    public void loadShopCategoryList() {
        mShopModel.loadShopCategory(
                mSupplierId,
                mShopCategoryView.getPagerTag(),
                new IShopModel.OnLoadShopCategoryListener() {
                    @Override
                    public void onSuccess(List<QueryShopCategoryResponse.ShopCategory> shopCategoryList) {
                        // 酒商无任何分类
                        if (shopCategoryList == null || shopCategoryList.size() == 0) {
                            return;
                        }

                        mShopCategoryList = shopCategoryList;
                        QueryShopCategoryResponse.ShopCategory firstShopCategory =
                                mShopCategoryList.get(0);
                        // 默认选中第一个
                        firstShopCategory.setSelected(true);
                        mShopCategoryView.setCategoryData(shopCategoryList);

                        // 默认显示第一个分类的二级分类
                        List<Tag> firstTags = firstShopCategory.getTags();
                        mShopCategoryView.setTagLayoutData(firstTags);
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }

    @Override
    public void onCategoryItemClick(int position) {
        for (QueryShopCategoryResponse.ShopCategory shopCategory : mShopCategoryList) {
            shopCategory.setSelected(false);
        }

        QueryShopCategoryResponse.ShopCategory shopCategory = mShopCategoryList.get(position);
        shopCategory.setSelected(true);
        mShopCategoryView.notifyDataSetChanged();

        List<Tag> tags = shopCategory.getTags();
        mShopCategoryView.setTagLayoutData(tags);
    }

    @Override
    public void toShopCategoryGoodsActivity(TagView tagView) {
        mShopCategoryView.toShopCategoryGoodsActivity(
                tagView.getTagDataName(), mSupplierId, tagView.getTagId());
    }
}
